package com.fengke.cn.yejian11.activity;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.fengke.cn.yejian11.Model.Model;
import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.fragment.AskFragment;
import com.fengke.cn.yejian11.fragment.VideoFragment;
import com.fengke.cn.yejian11.impls.TeaImpls;
import com.fengke.cn.yejian11.javabean.TeaInfo;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TeaDetailActivity extends AppCompatActivity {
    private ImageView pic;
    private String Id;
    private TextView intro,position;
    private RadioGroup radioGroup;
    private RadioButton tea_class,ask;
    private TeaInfo info;
    private VideoFragment videoFragment;
    private AskFragment  askFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tea_detail);
        init();
        event();
    }
    public void init()
    {
        Intent intent=getIntent();
        Id=intent.getStringExtra("tea_id");
        pic=(ImageView)findViewById(R.id.tea_detail_pic);
        radioGroup=(RadioGroup)findViewById(R.id.tea_detail_radiogroup);
        tea_class=(RadioButton)findViewById(R.id.tea_detail_class);
        ask=(RadioButton)findViewById(R.id.tea_detail_ask);
        position=(TextView)findViewById(R.id.tea_detail_position);
        intro=(TextView)findViewById(R.id.tea_detail_intro);
        videoFragment=new VideoFragment();
        askFragment=new AskFragment();
        fragmentManager=getSupportFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.tea_detail_frame,videoFragment);
        transaction.commit();

    }
    public void event()
    {
        Retrofit retrofit= Model.getConn();
        TeaImpls impls=retrofit.create(TeaImpls.class);
        Call<TeaInfo> call=impls.getTeaInfo(Id);
        call.enqueue(new Callback<TeaInfo>() {
            @Override
            public void onResponse(Call<TeaInfo> call, Response<TeaInfo> response) {
                info=response.body();
                Picasso.with(TeaDetailActivity.this).load(Config.PIC_URL+info.getThumb()).into(pic);
                String s=info.getIntroduction().replaceAll("</p>","").replaceAll("<p>","");
                intro.setText(s);
                position.setText(info.getProvince_name()+info.getCity_name()+info.getClub_name());
            }

            @Override
            public void onFailure(Call<TeaInfo> call, Throwable t) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case  R.id.tea_detail_class:
                        replace(videoFragment);
                        break;
                    case R.id.tea_detail_ask:
                        replace(askFragment);
                        break;
                }
            }
        });

    }
    private void replace(Fragment fragment)
    {
        fragmentManager=getSupportFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.tea_detail_frame,fragment);
        transaction.commit();
    }
}
